#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<mutex>
#include<fstream>
#include <boost/algorithm/string.hpp>
#include"./cppjieba/Jieba.hpp"
#include"Log.hpp"

namespace tool_a
{
    using namespace std;
    using namespace Log_Module;
    class file_tool
    {
        public:
        static bool ReadFile(const string&file_path,string* out)
        {
            std::ifstream in(file_path,ios::in);
            if(!in.is_open())
            {
                cerr<<"open file fail!"<<endl;
                return false;
            }
            string line;
            while(std::getline(in,line))
            {
                *out+=line;
            }
            in.close();
            return true;
        }       
    };

    class String_tool
    {
        public:
        //按sep进行分割
        static void Split(const string&t,vector<string>*result,const string&sep)
        {
            //保留空字符串
            boost::split(*result,t,boost::is_any_of(sep),boost::token_compress_on);
        }
    };

    const char* const DICT_PATH = "./dict/jieba.dict.utf8";
    const char* const HMM_PATH = "./dict/hmm_model.utf8";
    const char* const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char* const IDF_PATH = "./dict/idf.utf8";
    const char* const STOP_WORD_PATH = "./dict/stop_words.utf8";

    class Jieba_tool
    {

        
        private:
        cppjieba::Jieba jieba;
        Jieba_tool(const Jieba_tool&) = delete;
        std::unordered_map<std::string, bool> stop_words;
        Jieba_tool():jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH){}
        static Jieba_tool*instance;
        public:
        static Jieba_tool* get_instance()
        {
            static std::mutex mtx;
            if(nullptr == instance){
                mtx.lock();
                if(nullptr == instance){
                    instance = new Jieba_tool();
                    instance->InitJieba_tool();
                }
                mtx.unlock();
            }
            return instance;
        }
        void InitJieba_tool()
        {
            std::ifstream in(STOP_WORD_PATH);
            if(!in.is_open()){
                LOG(Log_Level::FATAL)<<"暂停词文件找不到";
                return;
            }

            std::string line;
            while(std::getline(in, line)){
                stop_words.insert({line, true});
            }
            in.close();
        }
        void CutStringHelper(const std::string &src, std::vector<std::string> *out)
            {
                jieba.CutForSearch(src, *out);
                for(auto iter = out->begin(); iter != out->end(); ){
                    auto it = stop_words.find(*iter);
                    if(it != stop_words.end()){
                        //说明当前的string 是暂停词，需要去掉
                        iter = out->erase(iter);
                    }
                    else{
                        iter++;
                    }
                }
            }
        static void Separate_String(const string&src,vector<string>*out)
        {
            tool_a::Jieba_tool::get_instance()->CutStringHelper(src,out);
        }
    };
    Jieba_tool *Jieba_tool::instance = nullptr;
}